/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import net.worlds.Main;
import net.worlds.Util;

/**
 *
 * @author Jacob Morgan
 */
public class EButton extends EComponent{
    private boolean resizeImage = false;
    protected boolean undecorated;
    protected EActionListener actionListener = null;
    protected String text = null;
    protected Color textColor = new Color(255, 255, 255);
    protected Color color;
    protected Color topColor;
    protected Color bottomColor;
    protected FontMetrics fontMetrics;
    protected Font font = EDGESettings.defaultFont;
    private int textHeight;
    private int textWidth;
    protected boolean swapColors = false;
    protected boolean alwaysShowEdges = false;
    protected boolean showEdges = false;
    private VolatileImage image;
    public EButton(String text)
    {
        setText(text);
        setColor(new Color(57, 79, 99));
    }
    public EButton(BufferedImage image)
    {
        setImage(image);
        setColor(new Color(57, 79, 99));
    }
    public EButton(BufferedImage image, int sx, int sy)
    {
        resizeImage = true;
        setImage(image, sx, sy);
        setColor(new Color(57, 79, 99));
    }
    public void setUndecorated(boolean v)
    {
        undecorated = v;
    }
    public void setActionListener(EActionListener al)
    {
        actionListener = al;
    }
    public void setAlwaysShowEdges(boolean v)
    {
        alwaysShowEdges = v;
    }
    public final void setText(String t)
    {
        this.text = t;
        Rectangle2D bounds = font.getStringBounds(text, Util.fontRendererContext);
        width = (int) bounds.getWidth() + 12;
        height = (int) bounds.getHeight() + 12;
        textHeight = (int) bounds.getHeight();
        textWidth = (int) bounds.getWidth();
    }
    public final void setImage(BufferedImage image, int sx, int sy)
    {
        if(Main.noView)
            return;
        this.image = Util.toVolatileImage(image);
        if(width == 0)
        {
            width = (int) sx + 12;
            height = (int) sy + 12;
        }
    }
    public final void setImage(BufferedImage image)
    {
        if(Main.noView)
            return;
        if(image != null)
        {
            this.image = Util.toVolatileImage(image);
            if(width == 0)
            {
                width = (int) image.getWidth() + 12;
                height = (int) image.getHeight() + 12;
            }
        }
        else
            this.image = null;
    }
    public final void setImageAndResize(BufferedImage image)
    {
        this.image = Util.toVolatileImage(image);
        width = (int) image.getWidth() + 12;
        height = (int) image.getHeight() + 12;
    }
    public final void setColor(Color c)
    {
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        if(r < 32)
          r = 32;
        if(g < 32)
            g = 32;
        if(b < 32)
            b = 32;
        if(r > 255 - 32)
            r = 255 - 32;
        if(g > 255 - 32)
            g = 255 - 32;
        if(b > 255 - 32)
            b = 255 - 32;
        c = new Color(r, g, b);
        this.color = c;
        this.topColor = new Color(c.getRed() + 32, c.getGreen() + 32, c.getBlue() + 32);
        this.bottomColor = new Color(c.getRed() - 32, c.getGreen() - 32, c.getBlue() - 32);
    }
    public void setTextColor(Color c)
    {
        textColor = c;
    }
    @Override
    public void mouseOver()
    {
        super.mouseOver();
        if(!mouseOver)
        {
            showEdges = true;
            setColor(topColor);
        }
    }
    @Override
    public void mouseOff()
    {
        if(mouseOver)
        {
            if(!(this instanceof EToggleButton && ((EToggleButton)this).down))
                showEdges = false;
            setColor(bottomColor);
        }
    }
    @Override
    public void mousePressed()
    {
        swapColors = true;
        if(actionListener != null)
            actionListener.actionPerformed();
    }
    @Override
    public void mouseReleased()
    {
        if(!(this instanceof EToggleButton && ((EToggleButton)this).down))
            swapColors = false;
    }
    public void setFont(Font font)
    {
        this.font = font;
    }
    @Override
    public void render(Graphics2D g)
    {
        super.render(g);
        int w = getWidth();
        int h = getHeight();
        if(!undecorated)
        {
            if((this instanceof EToggleButton && ((EToggleButton)this).down))
                g.setColor(topColor);
            else
                g.setColor(color);
            g.fillRect(getX(), getY(), w, h);
            if(showEdges)
            {
                if(swapColors)
                    g.setColor(bottomColor);
                else
                    g.setColor(topColor);
                g.drawLine(getX(), getY(), getX() + w, getY());
                g.drawLine(getX(), getY(), getX(), getY() + h);
                if(swapColors)
                    g.setColor(topColor);
                else
                    g.setColor(bottomColor);
                g.drawLine(getX(), getY() + h, getX() + w, getY() + h);
                g.drawLine(getX() + w, getY(), getX() + w, getY() + h);
            }
        }
        if(image == null && text != null)
            drawText(g, font, text,  textColor, getX()+ width/2 - textWidth/2, getY() + getHeight()/2 + textHeight/2);
        else if(image != null)
        {
            if(resizeImage)
                g.drawImage(image, getX() + 6, getY() + 6, width - 12, height - 12, null);
            else
                g.drawImage(image, getX() + w/2 - image.getWidth()/2, getY() + h/2 - image.getHeight()/2, null);
        }
    }
}
